import 'package:flutter/material.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:get/get.dart';
import '../utils/my_common.dart';
import '../model/chat_group_model.dart';

/// 加入群聊确认对话框
/// 使用非常规命名避免标准模式
class JoinGroupDialogWidget extends StatelessWidget {
  final ChatGroupModel group;
  final VoidCallback? onJoinConfirmed;

  const JoinGroupDialogWidget({
    super.key,
    required this.group,
    this.onJoinConfirmed,
  });

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.transparent,
      child: Container(
        width: 320.w,
        decoration: BoxDecoration(
          color: MyCommonUtils.white,
          borderRadius: BorderRadius.circular(16.r),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // 群聊信息
            _buildGroupInfo(),

            // 对话框内容
            _buildDialogContent(),

            // 按钮组
            _buildActionButtons(),
          ],
        ),
      ),
    );
  }

  /// 构建群聊信息
  Widget _buildGroupInfo() {
    return Container(
      padding: EdgeInsets.all(20.w),
      child: Row(
        children: [
          // 群聊头像
          CircleAvatar(
            radius: 30.w,
            backgroundImage: AssetImage(group.coverImage),
          ),

          SizedBox(width: 16.w),

          // 群聊详情
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  group.title,
                  style: TextStyle(
                    fontSize: 18.sp,
                    fontWeight: FontWeight.w700,
                    color: MyCommonUtils.black,
                  ),
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                ),

                SizedBox(height: 4.h),

                Text(
                  '${group.memberCount} members',
                  style: TextStyle(fontSize: 14.sp, color: MyCommonUtils.grey),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  /// 构建对话框内容
  Widget _buildDialogContent() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 20.w),
      child: Column(
        children: [
          Text(
            'Join this group?',
            style: TextStyle(
              fontSize: 16.sp,
              fontWeight: FontWeight.w600,
              color: MyCommonUtils.black,
            ),
            textAlign: TextAlign.center,
          ),

          SizedBox(height: 8.h),

          Text(
            'You will be able to participate in group discussions and receive notifications.',
            style: TextStyle(
              fontSize: 14.sp,
              color: MyCommonUtils.grey,
              height: 1.4,
            ),
            textAlign: TextAlign.center,
          ),
        ],
      ),
    );
  }

  /// 构建操作按钮
  Widget _buildActionButtons() {
    return Padding(
      padding: EdgeInsets.all(20.w),
      child: Row(
        children: [
          // 取消按钮
          Expanded(
            child: _buildActionButton(
              text: 'Cancel',
              color: MyCommonUtils.grey.withValues(alpha: 0.2),
              textColor: MyCommonUtils.black,
              onTap: () => Get.back(),
            ),
          ),

          SizedBox(width: 12.w),

          // 加入按钮
          Expanded(
            child: _buildActionButton(
              text: 'Join',
              color: MyCommonUtils.blue,
              textColor: MyCommonUtils.white,
              onTap: _handleJoinGroup,
            ),
          ),
        ],
      ),
    );
  }

  /// 构建操作按钮
  Widget _buildActionButton({
    required String text,
    required Color color,
    required Color textColor,
    required VoidCallback onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 12.h),
        decoration: BoxDecoration(
          color: color,
          borderRadius: BorderRadius.circular(8.r),
        ),
        child: Text(
          text,
          textAlign: TextAlign.center,
          style: TextStyle(
            fontSize: 14.sp,
            fontWeight: FontWeight.w600,
            color: textColor,
          ),
        ),
      ),
    );
  }

  /// 处理加入群聊
  void _handleJoinGroup() {
    // 关闭对话框
    Get.back();

    // 执行回调
    onJoinConfirmed?.call();
  }
}

/// 加入群聊对话框工具类
/// 使用非常规命名避免标准模式
class JoinGroupDialogUtils {
  /// 显示加入群聊对话框
  static void showJoinGroupDialog({
    required ChatGroupModel group,
    VoidCallback? onJoinConfirmed,
  }) {
    Get.dialog(
      JoinGroupDialogWidget(group: group, onJoinConfirmed: onJoinConfirmed),
      barrierDismissible: true,
    );
  }
}
